﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADC.DAL;
using log4net;
using System.Data;

namespace ADC.BL
{
    public class ProjectFactory
    {
        /// <summary>
        /// Get all visible
        /// </summary>
        /// <returns></returns>
        public static List<Project> GetAll() {
            var db = new dbADCEntities();
            return db.Projects.Where(d => d.IsVisibled != null && d.IsVisibled == true).OrderByDescending(d => d.ModifiedDate).ToList();
        }

        public static List<Project> ListAll()
        {
          var db = new dbADCEntities();
          return db.Projects.OrderByDescending(d => d.ModifiedDate).ToList();
        }

        public static List<Project> ListAllByProjectType(int projectTypeID)
        {
            var db = new dbADCEntities();
            return db.Projects.Where(d => d.ProjectTypeID == projectTypeID).ToList();
        }

        public static Project GetByID(int projectID)
        {
            var db = new dbADCEntities();
            if (db.Connection.State.Equals(ConnectionState.Closed))
            {
                db.Connection.Open();
            }
            return db.Projects.FirstOrDefault(g => g.ProjectID == projectID);
        }

        // Update Programe
        /// </summary>
        /// <param name="programe"></param>
        /// <returns></returns>
        public static int Update(Project project)
        {
            try
            {
                var db = new dbADCEntities();
                var objProject = new Project();
                objProject = db.Projects.Where(g => g.ProjectID == project.ProjectID).FirstOrDefault();
                objProject.Title = project.Title;
                objProject.ProjectTypeID = project.ProjectTypeID;
                objProject.BodyDetails = project.BodyDetails;
                objProject.Body = project.Body;
                objProject.ModifiedBy = project.ModifiedBy;
                objProject.ModifiedDate = DateTime.Now;
                objProject.IsVisibled = project.IsVisibled;
                if (!string.IsNullOrEmpty(project.ImagePath)) {
                    objProject.ImagePath = project.ImagePath;
                }
                
                return db.SaveChanges();
            }
            catch (Exception)
            {             
                // Log here
                throw;
            }
        }

      public  static int Delete(int projectID)
      {
        var db = new dbADCEntities();
        
        // Delete Programe
        var tourInfo = db.Projects.Where(g => g.ProjectID.Equals(projectID)).FirstOrDefault();
        db.DeleteObject(tourInfo);
        return db.SaveChanges();
      }

      public static Project Insert(Project tourInfo)
      {
        var db = new dbADCEntities();
        db.AddToProjects((tourInfo));
        db.SaveChanges();
        return tourInfo;
      }
    }
}
